package com.laoniu.biz.goods;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.request.GoodsStockRequestDto;
import com.laoniu.dto.response.GoodsReturnDto;
import com.laoniu.entity.*;
import com.laoniu.service.base.BaseGoodsService;
import com.laoniu.service.base.BaseSupplierService;
import com.laoniu.service.goods.GoodsReturnService;
import com.laoniu.service.goods.GoodsStockService;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class GoodsReturnBiz {

    @Autowired
    private GoodsReturnService goodsReturnService;
    @Autowired
    private BaseGoodsService goodsService;
    @Autowired
    private BaseSupplierService supplierService;
    @Autowired
    private GoodsStockService goodsStockService;

    public PageResponse<GoodsReturnDto> pageList(GoodsStockRequestDto requestDto) {
        Page<GoodsReturn> page = goodsReturnService.listPage(requestDto);
        PageResponse<GoodsReturnDto> result = new PageResponse<GoodsReturnDto>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        result.setTotal(page.getTotal());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Long,String> supplierMap = Maps.newHashMap();
            Map<Long,String> goodsMap = Maps.newHashMap();

            List<GoodsReturnDto> dtos = Lists.newArrayList();
            for (GoodsReturn record : page.getRecords()) {
                GoodsReturnDto dto = Convert.convert(GoodsReturnDto.class, record);
                //查询供应商名称
                if (supplierMap.containsKey(record.getSupplierId())){
                    dto.setSupplierName(supplierMap.get(record.getSupplierId()));
                }else {
                    BaseSupplier supplier = supplierService.getById(record.getSupplierId());
                    dto.setSupplierName(supplier.getName());
                    supplierMap.put(supplier.getId(), supplier.getName());
                }
                //查询商品名称
                if (goodsMap.containsKey(record.getGoodsId())){
                    dto.setGoodsName(goodsMap.get(record.getGoodsId()));
                }else {
                    BaseGoods goods = goodsService.getById(record.getGoodsId());
                    dto.setGoodsName(goods.getName());
                    goodsMap.put(goods.getId(), goods.getName());
                }
                dtos.add(dto);
            }
            result.setData(dtos);
        }
        return result;
    }

    public boolean returnGoods(GoodsStockRequestDto dto) {
        if (Objects.isNull(dto.getStockId()) || Objects.isNull(dto.getReturnNum())){
            return false;
        }
        GoodsStock goodsStock = goodsStockService.getById(dto.getStockId());
        if (Objects.isNull(goodsStock) || goodsStock.getNum() < dto.getReturnNum()){
            return false;
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        GoodsReturn goodsReturn = new GoodsReturn();
        goodsReturn.setSupplierId(goodsStock.getSupplierId());
        goodsReturn.setGoodsId(goodsStock.getGoodsId());
        goodsReturn.setPayType(goodsStock.getPayType());
        goodsReturn.setReturnTime(LocalDateTime.now());
        goodsReturn.setPrice(goodsStock.getPrice());
        goodsReturn.setNum(dto.getReturnNum());
        goodsReturn.setSumPrice(goodsStock.getPrice().multiply(new BigDecimal(dto.getReturnNum())));
        goodsReturn.setRemark(dto.getRemark());
        goodsReturn.setOpTime(LocalDateTime.now());
        goodsReturn.setOpId(user.getId());
        goodsReturn.setOpName(user.getName());
        goodsReturnService.save(goodsReturn);
        goodsService.addNum(goodsStock.getGoodsId(), 0 - dto.getReturnNum());
        return true;
    }
}
